#include "MainWidget.h"
#include "LoginDialog.h"
#include "ui_MainWidget.h"
#include <QStandardItem>
#include <QInputDialog>
#include <QMessageBox>
#include <QPainter>
#include <iostream>
using namespace std;

MainWidget::MainWidget(System* sys, QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    this->setStyleSheet("MainWindow { background-image: url(1.png); }");
    this->sys = sys;

    initUi();

    initModel();
    flushTable();
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::initModel()
{
    //创建QSqlTableModel
    model = new QSqlTableModel(this, sys->db);
    //令model既显示student又显示student2表
    model->setTable("account");

    model->setHeaderData(3, Qt::Horizontal, tr("用户1"));
    model->setHeaderData(5, Qt::Horizontal, tr("电话1"));
    model->setHeaderData(6, Qt::Horizontal, tr("用户2"));
    model->setHeaderData(8, Qt::Horizontal, tr("电话2"));
}

void MainWidget::flushTable()
{
    model->select();
    ui->tableView->setModel(model);
    ui->tableView->setColumnHidden(1, true); // 隐藏第3列（password字段）
    ui->tableView->setColumnHidden(4, true); // 隐藏第3列（password字段）
    ui->tableView->setColumnHidden(7, true); // 隐藏第3列（password字段）
    ui->tableView->show();
}

void MainWidget::flushWidget()
{
    ui->listWidget->clear();
    //将sys->account文本格式化成"账号：<account>"，并加入到listWidget中
    ui->listWidget->addItem(QString("账号：%1").arg(sys->account));
    //将sys->email文本格式化成"邮箱 Plaintiff@%1>"，并加入到listWidget中
    ui->listWidget->addItem(QString("邮箱：%1").arg(sys->email));
    //将sys->name1文本格式化成"用户: %1"，并加入到listWidget中
    ui->listWidget->addItem(QString("用户: %1").arg(sys->name1));
    //将sys->phone1文本格式化成"电话: %1"，并加入到listWidget中
    ui->listWidget->addItem(QString("电话: %1").arg(sys->phone1));
    //若name2非空，将sys->name2文本格式化成"用户: %1"，并加入到listWidget中
    if(sys->name2 != "")
        ui->listWidget->addItem(QString("用户2: %1").arg(sys->name2));
    //若phone2非空，将sys->phone2文本格式化成"电话: %1"，并加入到listWidget中
    if(sys->phone2 != "")
        ui->listWidget->addItem(QString("电话2: %1").arg(sys->phone2));
}

//账号查询
void MainWidget::on_pushButton_3_clicked()
{
    //格式化lineEdit的文本
    QString account = ui->lineEdit->text();

    if(account == "") {
        model->setFilter("");
        flushTable();
    } else {
        model->setFilter(QString("account = '%1'").arg(account));
        flushTable();
    }
}

//姓名查询
void MainWidget::on_pushButton_4_clicked()
{
    //格式化lineEdit_2的文本
    QString name = ui->lineEdit_2->text();

    if(name == "") {
        model->setFilter("");
        flushTable();
    } else {
        model->setFilter(QString("name1 = '%1' OR name2 = '%1'").arg(name));
        flushTable();
    }
}

//账号注销
void MainWidget::on_pushButton_clicked()
{
    //提示是否注销
    QMessageBox::StandardButton ret = QMessageBox::question(this, "Tip", "是否注销账号", QMessageBox::Yes | QMessageBox::No);

    if (ret == QMessageBox::Yes) {
        //注销账号
        QSqlQuery query(sys->db);
        query.prepare("DELETE FROM account WHERE account = ?");
        query.bindValue(0, sys->account);

        if (query.exec()) {
            // 注销成功提示
            QMessageBox::information(this, "Tip", "账号注销成功");

            // 关闭当前窗口
            close();
        } else {
            // 注销失败提示
            QMessageBox::critical(this, "Tip", "账号注销失败");
        }
    }
}

//修改密码
void MainWidget::on_pushButton_2_clicked()
{
    bool ok;
    QString newPassword = QInputDialog::getText(this, "修改密码", "请输入新的密码:", QLineEdit::Password, "", &ok);

    if (ok) {
        QSqlQuery query;
        query.prepare("UPDATE account SET password = ? WHERE account = ?");
        query.bindValue(0, newPassword); // 绑定新密码值
        query.bindValue(1, sys->account); // 绑定账户名称


        if (query.exec()) {
            //提示更新密码成功
            QMessageBox::information(this, "Tip", "更新密码成功");
            sys->password = newPassword;
            //flushTable();
        } else {
            //提示更新密码失败
            QMessageBox::critical(this, "Tip", "更新密码失败");
            //flushTable();
        }
    }
}

//退出按钮
void MainWidget::on_pushButton_5_clicked()
{
    close();
}

void MainWidget::initUi()
{
    ui->lineEdit->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 130);"
                                  "}");
    ui->lineEdit_2->setStyleSheet("QLineEdit {"
                                  "background-color: rgba(255, 255, 255, 130);"
                                  "}");
    //ui->tabWidget->setStyleSheet("background-color: rgba(255, 255, 255, 127);");
    //ui->tabWidget->setStyleSheet("QTabWidget:pane{border: 1px solid black; top: -1px;background: transparent;}QTabBar::tab{height:20px; margin-right: 2px; margin-bottom:-2px;}");
    ui->tabWidget->setStyleSheet(
        "QTabWidget::pane {"
        "   border: 1px solid black;"
        "   top: -1px;"
        "   background: rgba(255, 255, 255, 160);" // 50% transparent white
        "}"
        "QTabBar::tab {"
        "   height: 30px;"
        "   margin-right: 2px;"
        "   margin-bottom: -2px;"
        "   background: rgba(255, 255, 255, 160);" // You can also make the tabs semi-transparent
        "}"
        );

    ui->tableView->setStyleSheet("background-color: rgba(255, 255, 255, 160);");

    // 设置QTableView的样式
    ui->tableView->setStyleSheet("QTableView {"
                             "background-color: rgba(255, 255, 255, 160);"  // 127是alpha值
                             "}");
    // 设置QTableView表头的样式
    //ui->tableView->horizontalHeader()->setStyleSheet("background-color: rgba(255, 255, 255, 127);");
    //ui->tableView->verticalHeader()->setStyleSheet("background-color: rgba(255, 255, 255, 127);");
    ui->tableView->setStyleSheet(
        "QTableView {"
        "   background-color: rgba(255, 255, 255, 180);"  // 半透明背景
        "}"
        "QHeaderView::section {"
        "   background-color: rgba(255, 255, 255, 180);"  // 表头半透明背景
        "}"
        );


    // 设置QListWidget的样式
    ui->listWidget->setStyleSheet("QListWidget {"
                              "background-color: rgba(255, 255, 255, 190);"  // 127是alpha值
                              "}");


    ui->pushButton->setStyleSheet("background-color: rgba(255, 255, 255, 140);");
    ui->pushButton_2->setStyleSheet("background-color: rgba(255, 255, 255, 140);");
    ui->pushButton_3->setStyleSheet("background-color: rgba(255, 255, 255, 140);");
    ui->pushButton_4->setStyleSheet("background-color: rgba(255, 255, 255, 140);");
    ui->pushButton_5->setStyleSheet("background-color: rgba(255, 255, 255, 140);");
    ui->pushButton_6->setStyleSheet("background-color: rgba(255, 255, 255, 140);");
    ui->pushButton_7->setStyleSheet("background-color: rgba(255, 255, 255, 140);");


}

//修改邮箱地址
void MainWidget::on_pushButton_6_clicked()
{
    bool ok;
    QString newEmail = QInputDialog::getText(this, "修改邮箱地址", "请输入新的邮箱:", QLineEdit::Normal, "", &ok);

    if (ok) {
        QSqlQuery query;
        query.prepare("UPDATE account SET email = ? WHERE account = ?");
        query.bindValue(0, newEmail);
        query.bindValue(1, sys->account); // 绑定账户名称

        if (query.exec()) {
            QMessageBox::information(this, "Tip", "更新邮箱成功");
            sys->email = newEmail;
            flushTable();
            flushWidget();
        } else {
            QMessageBox::critical(this, "Tip", "更新邮箱失败");
            //flushTable();
        }
    }
}

//修改电话号
void MainWidget::on_pushButton_7_clicked()
{
    bool ok, ok2;
    QSqlQuery query;

    QString actNum = QInputDialog::getText(this, "选择账户", "输入1修改账户1电话号，输入2修改账户2电话号", QLineEdit::Normal, "", &ok2);

    if(actNum == "1") {
        QString newPhone = QInputDialog::getText(this, "修改电话号", "请输入新的电话:", QLineEdit::Normal, "", &ok);
        query.prepare("UPDATE account SET phone1 = ? WHERE account = ?");
        query.bindValue(0, newPhone);
        query.bindValue(1, sys->account); // 绑定账户名称
        if (query.exec()) {
            QMessageBox::information(this, "Tip", "更新电话成功");
            sys->phone1 = newPhone;
            flushTable();
            flushWidget();
        } else {
            //提示更新密码失败
            QMessageBox::critical(this, "Tip", "更新密码失败");
            //flushTable();
        }
    } else if(actNum == "2") {
        if(sys->phone2 != "") {
            QString newPhone = QInputDialog::getText(this, "修改电话号", "请输入新的电话:", QLineEdit::Normal, "", &ok);
            query.prepare("UPDATE account SET phone2 = ? WHERE account = ?");
            query.bindValue(0, newPhone);
            query.bindValue(1, sys->account); // 绑定账户名称
            if (query.exec()) {
                QMessageBox::information(this, "Tip", "更新电话成功");
                sys->phone2 = newPhone;
                flushTable();
                flushWidget();
            } else {
                //提示更新密码失败
                QMessageBox::critical(this, "Tip", "更新密码失败");
                //flushTable();
            }
        } else {
            QMessageBox::critical(this, "Tip", "单名账户无法修改电话2");
        }

    } else {
        QMessageBox::critical(this, "Tip", "请输入正确的内容");
    }

}
